package com.zaofan.dao;

import java.sql.SQLException;
import java.util.Collections;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zaofan.bean.Activity;
import com.zaofan.constant.StateConstant;
import com.zaofan.dao.base.DaoSupportImpl;

@SuppressWarnings("unchecked")
public class ActivityDao extends DaoSupportImpl<Activity> {
	private static Logger logger = LoggerFactory.getLogger(ActivityDao.class);
	public List<Activity> findTodayActivity(Integer shopId) {
		try {
			String sql = "select * from " + Activity.class.getSimpleName().toLowerCase()
					+ " where startDate <= curdate() and endDate >= curdate() and state = " + StateConstant.STATE_SHOW
					+ " and shopId = " + shopId;
			logger.info(sql);
			QueryRunner runner = new QueryRunner(getDataSource());
			return runner.query(sql, new BeanListHandler<>(Activity.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return Collections.EMPTY_LIST;
	}

	public List<Activity> findWholeActivity() {
		try {
			String sql = "select * from " + Activity.class.getSimpleName().toLowerCase()
					+ " where startDate <= curdate() and endDate >= curdate() and state = " + StateConstant.STATE_SHOW
					+ " and scopeLableCode = 'activityAll'";
			logger.info(sql);
			QueryRunner runner = new QueryRunner(getDataSource());
			return runner.query(sql, new BeanListHandler<>(Activity.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return Collections.EMPTY_LIST;
	}

	public List<Activity> findShopActivity() {
		try {
			String sql = "select * from " + Activity.class.getSimpleName().toLowerCase()
					+ " where startDate <= curdate() and endDate >= curdate() and state = " + StateConstant.STATE_SHOW
					+ " and scopeLableCode = 'activityShop'";
			logger.info(sql);
			QueryRunner runner = new QueryRunner(getDataSource());
			return runner.query(sql, new BeanListHandler<>(Activity.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return Collections.EMPTY_LIST;
	}

}
